﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Share_Insert_Update_AwardIssued : System.Web.UI.UserControl
{


    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request["AwardID"] != null || Request["PostingID"] != null)
        {
            fillForm();
            pn_Insert.Visible = false;
        }
        else
        {
            pn_Update.Visible = false;
            FillInsert();

        }


    }

    public void fillForm()
    {
        int awaID = int.Parse(Request["AwardID"].ToString());
        int postID = int.Parse(Request["PostingID"].ToString());
        int competID = int.Parse(Request["CompetitionID"].ToString());

        PostingDAO postDAO = new PostingDAO();
        CompetitionDAO competDAO = new CompetitionDAO();
        AwardDAO awaDAO = new AwardDAO();
        AwardInfo awaInfo = new AwardInfo();
        CompetitionInfo competInfo = new CompetitionInfo();
        AwardIssuedDAO awaiiDAO = new AwardIssuedDAO();

        awaInfo = awaDAO.GetAwardById(awaID);

        competInfo = competDAO.getCompetitionID(awaInfo.CompetitionId);

        lblCompetitionName.Text = competInfo.CompetitionName;
        lblAwardName.Text = awaInfo.AwardName;

        drd_Posting.DataSource = postDAO.GetPostingInfoByCompetitionID(awaInfo.CompetitionId);
        drd_Posting.DataBind();

        // PostingInfo postInfo = postDAO.GetPostingById(Int64.Parse(Request["PostingID"].ToString()));
        PostingInfo postInfo = postDAO.GetPostingById(awaiiDAO.GetPostID(awaID));
        lbl_PostName.Text = postInfo.PostingName;

    }


    protected void btn_Insert_Click(object sender, EventArgs e)
    {

        try
        {
            AwardIssuedDAO awaissDAO = new AwardIssuedDAO();
            AwardIssuedInfo awaissInfo = new AwardIssuedInfo();
            CompetitionDAO competDAO = new CompetitionDAO();


            awaissInfo.AwardId = int.Parse(ddl_AwardName.SelectedValue.ToString());
            awaissInfo.PostingId = Int64.Parse(ddl_PostingName.SelectedValue.ToString());
            if (awaissDAO.Insert(awaissInfo) > 0)
            {
                lblMessage.Text = "Insert Succesful!";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Successful!');", true);

            }


            else
            {
                lblMessage.Text = "Insert Failed! The Posting already have Prize!";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Failed!');", true);
            }

        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    protected void btn_Update_Click(object sender, EventArgs e)
    {
        try
        {

            AwardIssuedDAO awaissDAO = new AwardIssuedDAO();
            AwardIssuedInfo awaissInfo = new AwardIssuedInfo();


            awaissInfo.AwardId = int.Parse(Request["AwardID"].ToString());
            awaissInfo.PostingId = Int64.Parse(drd_Posting.SelectedValue.ToString());
            if (awaissDAO.Update(awaissInfo) > 0)
            {
                lblMessage.Text = "Update Succesful!";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successful!');", true);
                fillForm();
            }
            else
            {
                lblMessage.Text = "Update Failed! The Posting already have Prize! ";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Failed!');", true);
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/MaintainAwardsIssued.aspx");
    }
    protected void btn_Cancel2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/MaintainAwardsIssued.aspx");
    }


    public void FillInsert()
    {
        int competID = int.Parse(Request["CompetitionID"].ToString());
        CompetitionInfo competInfo = new CompetitionInfo();
        CompetitionDAO competDAO = new CompetitionDAO();
        competInfo = competDAO.getCompetitionID(competID);
        lbl_Compet_Name.Text = competInfo.CompetitionName;
       
        AwardDAO awaDAO = new AwardDAO();
        PostingDAO postDAO = new PostingDAO();
        ddl_AwardName.DataSource = awaDAO.GetAwardOfCompetition(competID);
        ddl_AwardName.DataBind();
        ddl_PostingName.DataSource = postDAO.GetPostingInfoByCompetitionID(competID);
        ddl_PostingName.DataBind();

    }
}

    